
$(document).ready(function () {	
/*	
	$('#add_plan').click(function () {
		//savePlan();
		//document.planForm.submit();
	});
*/
	$("#dialog").dialog("destroy");
	$("#dialog-form").dialog({
		autoOpen: false,
		width: 460,
		modal: true,
		resizable: false,
	    close: function() {
			$("#user-form input").removeClass('invalid');
			$("#user-form label").remove('.invalid');
			
			//test1;
			var myFileArr=window.frames["uploadFrame"].getFileArray();
			$.each(myFileArr,function(i,n){
				var htmlStr="";
				htmlStr+="<div><img src='lzm/images/doc.gif'/><span class='fn'>"
				htmlStr+=n.originName;
				htmlStr+="</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				htmlStr+="<span class='crtName' style='display:none;'>";
				htmlStr+=n.crtName;
				htmlStr+="</span><span class='dell'>删除</span></div>";
				$('#paListDIV').append(htmlStr);
			});
			$('#planb').val(goodFileAnnex());	
			$('#pland').val(goodFileAnnex1());	
			//alert($('#pland').val());	
			$('.dell').unbind("click");
			$('.dell').click(function () {
				$(this).parent().remove();
				$('#planb').val(goodFileAnnex());
				$('#pland').val(goodFileAnnex1());	
			});
			//alert(myFileArr.length);
			//getPaList();
	 	}
	});
	$('#add_plan_annex')
		.click(function() {
			$('#dialog-form').dialog('open');
	});
	$("#planForm").validate({
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
            form.submit();   //提交表单
            //alert("ggggg");  
            //window.location.href="save_plan_succ.jsp";
            //setTimeout(navigateSucc,1000);
        },
		rules: {			
			//'plan.PName':'required',
			'plan.PName':{
				required:true,
				maxlength: 50
			},
			//'plan.PSummary':'required',
			'plan.PSummary':{
				required:true,
				maxlength: 50
			},
			//'plan.PUnit':'required',
			'plan.PUnit':{
				required:true,
				maxlength: 20
			},
			//'plan.PContractor':'required',
			'plan.PContractor':{
				required:true,
				maxlength: 20
			},
			//'plan.PVersion':'required',
			'plan.PVersion':{
				required:true,
				digits:true,
				maxlength: 3
			},
			'plan.PTime':'required',
			'plankeywords':'required'
		},
		messages: {			
			'plan.PName':"*50字之内",
			'plan.PSummary':"*50字之内",
			'plan.PUnit':"*10字之内",
			'plan.PContractor':"*10字之内",
			'plan.PTime':"*",
			'plan.PVersion':"数字,1000以内",
			'plankeywords':"*必须选"
		}
	});
	
	$('#addStanderd').click(function(){
		window.open("/ag/hmk/findPlanStandard.jsp?", null,
									"scrollbars = yes,resizable = yes");
	});
});

function goodFileAnnex1(){
	var aa=1;
	var fileStr="";
	$('#paListDIV .fn').each(function(i,n){
		//alert(n.tbRole.RName);
		//uRoles[i] = "  "+n.tbRole.RName ;
		//alert($(this).text());
		if(aa==1){
			aa=2;
		}else{
			fileStr+=",";
		}
		fileStr+=$(this).text();
	});
	return fileStr;
}

function goodFileAnnex(){
	var aa=1;
	var fileStr="";
	$('#paListDIV .crtName').each(function(i,n){
		//alert(n.tbRole.RName);
		//uRoles[i] = "  "+n.tbRole.RName ;
		//alert($(this).text());
		if(aa==1){
			aa=2;
		}else{
			fileStr+=",";
		}
		fileStr+=$(this).text();
	});
	return fileStr;
}

function navigateSucc(){
	window.location.href="save_plan_succ.jsp";
}

/*
function getPaList(){	
	var timestamp = Date.parse(new Date());
	$.get("lzm/Session_findPaList.action?ts="+timestamp,null,getPaListRst);
}

function getPaListRst(data){
	$.each(data.paList,function(i,n){
		var htmlStr="";
		htmlStr+="<div><img src='lzm/images/doc.gif'/><span class='fn'>"
		htmlStr+=n.paName;
		htmlStr+="</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='dell'>删除</span></div>";
		$('#paListDIV').append(htmlStr);
	});
}*/

/**
 * 作增加预案
 */
function savePlan(){
	var aa=1;
	var fileStr="";
	$('#paListDIV .crtName').each(function(i,n){
		//alert(n.tbRole.RName);
		//uRoles[i] = "  "+n.tbRole.RName ;
		//alert($(this).text());
		if(aa==1){
			aa=2;
		}else{
			fileStr+=",";
		}
		fileStr+=$(this).text();
	});
	alert(fileStr);
	/*
	var url = 'lzm/SavePlan_savePlan.action';
	var params = {
			'plan.PName':$('#PName').attr('value'),
			'plan.tbPlanType.ptId':$('#planType').attr('value'),
			'plan.tbPlanGrade.pgId':$('#planGrade').attr('value'),
			'plan.tbEventType.etId':$('#eventType').attr('value'),
			'plan.tbPlanScope.psId':$('#planScope').attr('value'),
			'plan.tbPlanRegion.prId':$('#planRegion').attr('value'),
			'plan.PSummary':$('#PSummary').attr('value'),
			'plan.PUnit':$('#PUnit').attr('value'),
			'plan.PContractor':$('#PContractor').attr('value'),
			'plan.PTime':$('#PTime').attr('value'),
			'plan.PVersion':$('#PVersion').attr('value')
	}; 
	jQuery.post(url, params, savePlanRst, 'json');*/
}

/**
 * 作增加预案回调函数
 */
function savePlanRst(data){
	alert("gggg");
	alert(data.ajaxRst);
}